<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="班级名称" prop="banjimingcheng">
        <el-input v-model="dataForm.banjimingcheng" placeholder="班级名称"></el-input>
      </el-form-item>
      <el-form-item label="班主任" prop="banzhuren">
        <el-select v-model="dataForm.banzhuren" placeholder="请选择班主任">
          <el-option
            v-for="item in laoshiOptions"
            :key="item.id"
            :label="item.laoshixingming"
            :value="item.laoshixingming">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="人数" prop="renshu">
        <el-input-number v-model="dataForm.renshu" :min="0" :max="100" label="人数"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        banjimingcheng: '',
        banzhuren: '',
        renshu: 0,
        chuangjianshijian: ''
      },
      dataRule: {
        banjimingcheng: [
          { required: true, message: '班级名称不能为空', trigger: 'blur' }
        ],
        banzhuren: [
          { required: true, message: '班主任不能为空', trigger: 'change' }
        ],
        renshu: [
          { required: true, message: '人数不能为空', trigger: 'blur' }
        ]
      },
      laoshiOptions: []
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/banji/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.banjimingcheng = data.banji.banjimingcheng
              this.dataForm.banzhuren = data.banji.banzhuren
              this.dataForm.renshu = data.banji.renshu
              this.dataForm.chuangjianshijian = data.banji.chuangjianshijian
            }
          })
        }
        // 获取教师列表
        this.$http({
          url: this.$http.adornUrl('/laoshi/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 100
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.laoshiOptions = data.page.list
          }
        })
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/banji/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'banjimingcheng': this.dataForm.banjimingcheng,
              'banzhuren': this.dataForm.banzhuren,
              'renshu': this.dataForm.renshu,
              'chuangjianshijian': this.dataForm.chuangjianshijian
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-dialog {
  .el-form {
    .el-form-item {
      .el-input {
        width: 100%;
      }
    }
  }
}
</style> 